package com.lgh.ibs.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
	
	public static void traverseFolder2(String path) {

        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath());
                    } else {
                        System.out.println("文件:" + file2.getAbsolutePath());
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }

	public static List<File> getFile(String strPath, String fix, int dirLevel) {
		int countLevel = 1;//当前目录层级
		List<File> filelist = new ArrayList<File>();
		getFileList(strPath, fix, filelist, dirLevel, countLevel);
		return filelist;
	}
	
	private static List<File> getFileList(String strPath, String fix, List<File> filelist, int dirLevel, int countLevel) {
        File dir = new File(strPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                	if(countLevel <= dirLevel){
                		countLevel ++;
                		getFileList(files[i].getAbsolutePath(), fix, filelist, dirLevel, countLevel); // 获取文件绝对路径
                	}
                } else if (fileName.endsWith(fix)) { // 判断文件名是否以.avi结尾
                    String strFileName = files[i].getAbsolutePath();
                    System.out.println("---" + strFileName);
                    filelist.add(files[i]);
                } else {
                    continue;
                }
            }

        }
        return filelist;
    }
	
	public static void main(String[] args) {
		getFile("E:\\workspace_group\\2016_08_work\\2016-11-14_thempoint_dev", "xml",3);
	}
}
